package com.zhxh.nzpos.setting;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.zhxh.nzpos.api.ConnectorGoods;
import com.zhxh.nzpos.api.ConnectorPosID;
import com.zhxh.nzpos.api.Thread;
import com.zhxh.nzpos.dao.CategoryDao;
import com.zhxh.nzpos.dao.GoodsDao;
import com.zhxh.nzpos.model.Category;
import com.zhxh.nzpos.model.Goods;
import com.zhxh.nzpos.utils.DLog;
import com.zhxh.nzpos.utils.PersistenceUtil;

public class LoadGoodsThread extends Thread<String> {

	private final ConnectorGoods connectorProjects;
	private final GoodsDao goodsDao;
	private final CategoryDao categoryDao;
	ImageView refresh;
	ProgressBar progressBar;

	public LoadGoodsThread(View view, InitSettingActivity projectListActivity,
			ConnectorGoods connectorProjects, ProgressBar progressBar) {
		super(view, projectListActivity);

		this.connectorProjects = connectorProjects;
		goodsDao = new GoodsDao(context);
		categoryDao = new CategoryDao(context);
		this.progressBar = progressBar;
		refresh = projectListActivity.getRefreshButton();
		context = projectListActivity;
	}

	@Override
	protected void onPreExecute() {
		refresh.setEnabled(false);
		System.out.println("refresh set enabled: false");
		goodsDao.deleteAll();
		categoryDao.deleteAll();
		super.onPreExecute();
	}

	protected synchronized String doInBackground(Integer... params) {
		DLog.i("ProjectListActivity", "doInBackground() <-- i'm here");

		try {
			// TODO Trzeba obsłużyć czy uzytkownik chce pobierać rysunki do
			// projektów
			ConnectorPosID posIdConnector = new ConnectorPosID();
			String card = PersistenceUtil.getOperatorCard(context);
			String posId = posIdConnector.getPosID(card);
			PersistenceUtil.savePosId(posId, context);
			Category[] categorys = connectorProjects.getAllCategories();
			int i = 0;
			for (Category category : categorys) {
				i++;
				DLog.i("LoadGoodsThread..", category.getCategory());
				categoryDao.save(category);
				if(i < 30)
				 publishProgress(i);  
			}
			
			Goods[] goods= connectorProjects.getGoods();
			for (Goods good : goods) {
				String cateName  = "";
				i++;
				for(Category category : categorys) {
					if(good.getCategoryId().equals(category.getId())) {
						cateName =  category.getCategory();
						break;
					}
				}
				good.setCategoryName(cateName);
				goodsDao.save(good);
				if(i< 100) {
					publishProgress(i); 
				}
			}
			publishProgress(100); 
			return "";
		} catch (Exception e) {
			setException(e);
		}
		return null;
	}

	protected synchronized void onPostExecute(String result) {

		//
	 
		showFailInformation();
		 Toast.makeText(context, "初始化成功！", Toast.LENGTH_SHORT).show();
		refresh.setEnabled(true);
		System.out.println("refresh set enabled: true");
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		// TODO Auto-generated method stub
		//super.onProgressUpdate(values);
		
		  int vlaue = values[0]; 
		progressBar.setProgress(vlaue);  
	}
}
